package testmyself;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class HttpServer03 {
	private static final ExecutorService pool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()+2);
	public static void main(String[]args){
		ServerSocket server;
		try {
			server = new ServerSocket(8803);
			while(true) {
				final Socket client = server.accept();
				pool.execute(new Runnable() {
					public void run() {
						service(client);
					}
				});
				
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private static void service(Socket client) {
		try {
			PrintWriter pw =  new PrintWriter(client.getOutputStream(), true);
			pw.println("HTTP/1.1 200 OK");
			pw.println("Content-Type:text/html;charset=utf-8");
			String body = "hello HttpServer03";
			pw.println("Content-Length:"+body.getBytes().length);
			pw.println();
			pw.write(body);
			pw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
